package com.business_clean.app.flutter;

import android.app.Activity;
import android.content.Intent;
import android.view.WindowManager;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.DeviceUtils;
import com.business_clean.app.App;
import com.business_clean.app.config.ConstantMMVK;
import com.business_clean.app.uitl.MMKVHelper;
import com.idlefish.flutterboost.FlutterBoost;
import com.idlefish.flutterboost.FlutterBoostDelegate;
import com.idlefish.flutterboost.FlutterBoostRouteOptions;
import com.idlefish.flutterboost.containers.FlutterBoostActivity;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import io.flutter.embedding.android.FlutterActivityLaunchConfigs;

public class BoostDelegate implements FlutterBoostDelegate {

    public BoostDelegate() {
    }

    //这里是Flutter 跳转 原生
    @Override
    public void pushNativeRoute(FlutterBoostRouteOptions options) {
        Activity activity = FlutterBoost.instance().currentActivity();
        if (activity == null) {
            return;
        }
        Intent intent = new Intent();
        if (options.arguments() != null) {
            Set<String> keys = options.arguments().keySet();
            for (String key : keys) {
                intent.putExtra(key, (Serializable) options.arguments().get(key));
            }
        }
        //这里根据options.pageName来判断你想跳转哪个页面，这里简单给一个

        activity.startActivityForResult(intent, options.requestCode());
    }


    @Override
    public void pushFlutterRoute(FlutterBoostRouteOptions options) {//这里是原生跳转 Flutter
        Activity activity = FlutterBoost.instance().currentActivity();
        if (activity == null) {
            return;
        }
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("serverType", FlutterManager.sServerType);
        arguments.put("xmjztoken", MMKVHelper.getString(ConstantMMVK.TOKEN));
        arguments.put("xmjzversion", AppUtils.getAppVersionName());
        arguments.put("xmjzdevice", DeviceUtils.getManufacturer() + DeviceUtils.getModel());
        arguments.put("project_uuid", App.appViewModelInstance.getProjectInfo().getValue().getUuid());//项目的 uuid

        if (options.arguments() != null && options.arguments().size() > 0) {
            arguments.putAll(options.arguments());
        }

        Intent intent = new FlutterBoostActivity.CachedEngineIntentBuilder(XmFlutterBoostActivity.class)
                .backgroundMode(FlutterActivityLaunchConfigs.BackgroundMode.transparent)
                .destroyEngineWithActivity(false)
                .uniqueId(options.uniqueId())
                .url(options.pageName())
                .urlParams(arguments)
                .build(activity);
        activity.startActivity(intent);
    }
}
